package com.example.demo8.Student;

import com.example.demo8.dao.ITaskDao;
import com.example.demo8.dao.Impl.ITaskDaoImpl;
import com.example.demo8.entity.Task;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@WebServlet({"/taskStudent","/getById","/update","/delete","/add","/deleteAll"})
public class TaskStudent extends HttpServlet {
    ITaskDao iTask = new ITaskDaoImpl();
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String method = request.getServletPath();
        switch (method){
            //表单显示数据和名称查询
            case "/taskStudent":
                String taskName = request.getParameter("taskName");
                int currentPage = 1;
                if (request.getParameter("page") != null) {
                    try {
                        currentPage = Integer.parseInt(request.getParameter("page"));
                    } catch (NumberFormatException e) {
                        // 处理非数字页码的情况，默认为第一页
                        currentPage = 1;
                    }
                }

                int pageSize = 5; // 每页显示的记录数
                int totalRecords = iTask.getCount(); // 获取总记录数
                int totalPages = (int) Math.ceil((double) totalRecords / pageSize);

                int start = (currentPage - 1) * pageSize;
                int end = currentPage * pageSize;

                List<Task> taskList;
                if (taskName != null && !taskName.isEmpty()) {
                    taskList = iTask.getAllByName(taskName);
                } else {
                    taskList = iTask.getAllByPage(start, end);
                }

                request.setAttribute("taskList", taskList);
                request.setAttribute("currentPage", currentPage);
                request.setAttribute("totalPages", totalPages);
                request.getRequestDispatcher("taskStudent.jsp").forward(request, response);
                return; // 避免后续代码执行

            case "/getById":
                //按id查询
                request.setAttribute("task",iTask.getById(Integer.parseInt(request.getParameter("taskId"))));
                request.setAttribute("taskList",iTask.getAll());
                request.getRequestDispatcher("taskById.jsp").forward(request,response);
                break;
            case "/delete":
                iTask.delete(Integer.parseInt(request.getParameter("taskId")));
                request.setAttribute("taskList",iTask.getAll());
                break;
            case "/deleteAll":
                iTask.deleteAll();
                break;
                }

        request.setAttribute("taskList",iTask.getAll());
        request.getRequestDispatcher("taskStudent.jsp").forward(request,response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String method = request.getServletPath();
        request.setCharacterEncoding("UTF-8");
        String taskName = request.getParameter("taskName");
        String taskGroup = request.getParameter("taskGroup");
        String taskContent = request.getParameter("taskContent");
        String taskExpression = request.getParameter("taskExpression");
        Integer taskState = Integer.parseInt(request.getParameter("taskState"));
        LocalDate taskDate = LocalDate.parse(request.getParameter("taskDate"));
        Integer taskStrategy = Integer.parseInt(request.getParameter("taskStrategy"));
        Integer taskCurrent = Integer.parseInt(request.getParameter("taskCurrent"));
        String taskDesc = request.getParameter("taskDesc");
        switch (method){
            case "/update":
                Integer taskId = Integer.parseInt(request.getParameter("taskId"));
                iTask.update(new Task(taskId,taskName,taskGroup,taskContent,taskExpression,taskState,taskDate,taskStrategy,taskCurrent,taskDesc));
                break;
            case "/add":
                iTask.add(new Task(null,taskName,taskGroup,taskContent,taskExpression,taskState,taskDate,taskStrategy,taskCurrent,taskDesc));
                break;
        }
        request.setAttribute("taskList",iTask.getAll());
        request.getRequestDispatcher("taskStudent.jsp").forward(request,response);
    }
}
